package org.springframework.boot.tests.hibernate52.mytest.custombean;


import org.springframework.boot.tests.hibernate52.mytest.annotation.Chris;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;


@Chris(lazy = true)
public class MyApplicationListenerTest implements GenericApplicationListener {


	@Override
	public boolean supportsEventType(ResolvableType eventType) {
		Class<?> type = eventType.getRawClass();
		if (type == null) {
			return false;
		}
		return ContextRefreshedEvent.class.isAssignableFrom(type);
	}

	@Override
	public void onApplicationEvent(@Nullable ApplicationEvent event) {
		if (event != null) {
			if (event instanceof ContextRefreshedEvent) {
				ApplicationContext applicationContext = (ApplicationContext) event.getSource();
				ManualRegisterBean manualRegisterBean = (ManualRegisterBean) applicationContext.getBean(ManualRegisterBean.class.getName());
				manualRegisterBean.onContextRefreshedEvent();
			}
		}
	}

	@Override
	public boolean supportsSourceType(Class<?> sourceType) {
		return ApplicationContext.class.isAssignableFrom(sourceType);
	}

}
